package com.eastfair.venueservice.service.impl;

import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.SettlementRecordMapper;
import com.eastfair.venueservice.entity.ComplaintsAndEvaluation;
import com.eastfair.venueservice.entity.SettlementRecord;
import com.eastfair.venueservice.service.SettlementRecordService;
import com.eastfair.boot.service.SuperServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;

//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

import com.eastfair.venueservice.vo.*;
import com.eastfair.venueservice.dto.*;

/**
 * <p>
 * 业务实现类
 * 结算记录
 * </p>
 *
 * @author dqq
 * @date 2022-12-28
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class SettlementRecordServiceImpl extends SuperServiceImpl<SettlementRecordMapper, SettlementRecord> implements SettlementRecordService {

    @Override
    protected R<Boolean> handlerSave(SettlementRecord model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<SettlementRecord> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<SettlementRecord> list(SettlementRecordDTO modelDTO, List<Long> ids) {
        return ((SettlementRecordService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'exhibitionManageId', #args[0]?.exhibitionManageId}," +
                            "{'settlementNo', #args[0]?.settlementNo}," +
                            "{'systemType', #args[0]?.systemType}," +
                            "{'companyName', #args[0]?.companyName}," +
                            "{'receivables', #args[0]?.receivables}," +
                            "{'received', #args[0]?.received}," +
                            "{'settlementAmount', #args[0]?.settlementAmount}," +
                            "{'invoicingStatus', #args[0]?.invoicingStatus}," +
                            "{'invoicedAmount', #args[0]?.invoicedAmount}," +
                            "{'refundStatus', #args[0]?.refundStatus}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<SettlementRecord> listOfCache(SettlementRecordDTO modelDTO, List<Long> ids) {
        //查询
        QueryWrapper<SettlementRecord> queryWrapper = new QueryWrapper<>();
        if (modelDTO != null) {
            queryWrapper.lambda().eq(modelDTO.getExhibitionManageId() != null, SettlementRecord::getExhibitionManageId, modelDTO.getExhibitionManageId());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getSettlementNo()), SettlementRecord::getSettlementNo, modelDTO.getSettlementNo());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getSystemType()), SettlementRecord::getSystemType, modelDTO.getSystemType());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getCompanyName()), SettlementRecord::getCompanyName, modelDTO.getCompanyName());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getReceivables()), SettlementRecord::getReceivables, modelDTO.getReceivables());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getReceived()), SettlementRecord::getReceived, modelDTO.getReceived());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getSettlementAmount()), SettlementRecord::getSettlementAmount, modelDTO.getSettlementAmount());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getInvoicingStatus()), SettlementRecord::getInvoicingStatus, modelDTO.getInvoicingStatus());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getInvoicedAmount()), SettlementRecord::getInvoicedAmount, modelDTO.getInvoicedAmount());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getRefundStatus()), SettlementRecord::getRefundStatus, modelDTO.getRefundStatus());
        }
        if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(SettlementRecord::getId, ids);
        }
        queryWrapper.lambda().eq(SettlementRecord::getIsDeleted, BusinessConstant.DELETE_NO);
        List<SettlementRecord> modelList = list(queryWrapper);
        return modelList;
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public SettlementRecordVO doToVo(SettlementRecord model) {
        SettlementRecordVO modelVo = new SettlementRecordVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<SettlementRecordVO> doToVos(List<SettlementRecord> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<SettlementRecordVO> result = new ArrayList<>();
        for (SettlementRecord model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }
}
